using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace cb0tServer
{
    class Muzzles
    {
        private static List<MuzzledUserItem> muzzle_items = new List<MuzzledUserItem>();

        public static void CheckTimeouts()
        {
            int tn = (int)cb0tServer.Helpers.UnixTime();

            for (int x = 0; x < muzzle_items.Count; x++)
            {
                if ((muzzle_items[x].muzzle_time + (Settings.mtimeout * 60)) < tn)
                {
                    UserObject u = UserPool.GetUserByName(muzzle_items[x].name);

                    if (u != null)
                    {
                        if (u.physical_ip.Equals(muzzle_items[x].ip) ||
                            u.guid.Equals(muzzle_items[x].guid))
                        {
                            u.muzzled = false;
                        }
                    }

                    UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(98).Replace("+n", muzzle_items[x].name)));
                    muzzle_items.RemoveAt(x);
                    return;
                }
            }
        }

        public static bool IsMuzzled(UserObject userobj)
        {
            foreach (MuzzledUserItem m in muzzle_items)
            {
                if (m.guid.Equals(userobj.guid) || m.ip.Equals(userobj.physical_ip))
                {
                    return true;
                }
            }

            return false;
        }

        public static void AddMuzzle(UserObject userobj)
        {
            foreach (MuzzledUserItem m in muzzle_items)
            {
                if (m.guid.Equals(userobj.guid) ||
                    m.ip.Equals(userobj.physical_ip))
                {
                    return;
                }
            }

            MuzzledUserItem mu = new MuzzledUserItem();
            mu.name = userobj.name;
            mu.guid = userobj.guid;
            mu.ip = userobj.physical_ip;
            mu.muzzle_time = (int)cb0tServer.Helpers.UnixTime();
            muzzle_items.Add(mu);
        }

        public static void RemMuzzle(UserObject userobj)
        {
            for (int i = 0; i < muzzle_items.Count; i++)
            {
                if (muzzle_items[i].guid.Equals(userobj.guid) ||
                    muzzle_items[i].ip.Equals(userobj.physical_ip))
                {
                    muzzle_items.RemoveAt(i);
                    return;
                }
            }
        }

        public static void ClearMuzzles()
        {
            muzzle_items.Clear();

            foreach (UserObject u in UserPool.Users)
            {
                if (u.IsSet())
                {
                    if (u.logged_in)
                    {
                        if (u.muzzled)
                        {
                            u.muzzled = false;
                        }
                    }
                }
            }
        }

    }

    class MuzzledUserItem
    {
        public String name;
        public IPAddress ip;
        public Guid guid;
        public int muzzle_time;
    }
}
